﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Library.AppWeb;
using MPR.Poker.DALEntity.SYS;
using MPR.Poker.Model.LIS;
using System.Data;

namespace MPR.Poker.DALEntity.LIS
{
    public class DAL_Currency
    {
        public DAL_Currency()
        {
        }

        #region Insert/Update/Delete

        /// <summary>
        /// Thêm Tiền Tệ
        /// </summary>
        /// <param name="objCurrency"></param>
        /// <returns></returns>
        public ResultMessage Insert(LIS_Currency objCurrency)
        {
            ResultMessage objResultMessage = new ResultMessage();
            objResultMessage = CheckExist(objCurrency);
            if (objResultMessage.IsError) return objResultMessage;

            objResultMessage = InsertNoCheck(objCurrency);

            return objResultMessage;
        }

        /// <summary>
        /// Kiểm Tra Tiền Tệ Đã Tồn Tại
        /// </summary>
        /// <param name="strIDSys"></param>
        /// <param name="strCurrencyID"></param>
        /// <returns></returns>
        public ResultMessage CheckExist(LIS_Currency objCurrency)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                    LIS_Currency Currency = db.LIS_Currencys.FirstOrDefault(i => i.IsDeleted == false & i.CurrencyID == objCurrency.CurrencyID & i.IDSys != objCurrency.IDSys);

                    if (Currency != null && Currency.IDSys > 0)
                    {
                        objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.No_Error, "Tiền Tệ đã tồn tại", string.Empty);
                    }
                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.CheckData, "Lỗi kiểm tra tồn tại Tiền Tệ", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_Currency --> CheckExist");
                return objResultMessage;
            }
            return objResultMessage;
        }

        /// <summary>
        /// Thêm mới Tiền Tệ chưa kiểm tra tồn tại
        /// </summary>
        /// <param name="objCurrency"></param>
        /// <returns></returns>
        public ResultMessage InsertNoCheck(LIS_Currency objCurrency)
        {
            ResultMessage objResultMessage = new ResultMessage();
            LIS_Currency Currency = new LIS_Currency();

            try
            {
                using (PokerLife db = new PokerLife())
                {
                    if (objCurrency.IsDefault)
                    {
                        var list = db.LIS_Currencys.Where(p => p.IsDefault).ToList();
                        list.ForEach(p => p.IsDefault = false);
                        db.SaveChanges();
                    }

                    Currency.CurrencyID = objCurrency.CurrencyID;
                    Currency.CurrencyName = objCurrency.CurrencyName;
                    Currency.ExchangeRate = objCurrency.ExchangeRate;
                    Currency.Note = objCurrency.Note;
                    Currency.IsDefault = objCurrency.IsDefault;
                    Currency.IsActived = objCurrency.IsActived;
                    Currency.OrderIndex = db.LIS_Currencys.Count() + 1;
  
                    db.LIS_Currencys.Add(Currency);
                    db.SaveChanges();
                }

                objCurrency.IDSys = Currency.IDSys;
                DataCache.ClearDataCache.CurrencyCache();
                DAL_Event.Insert("Thêm mới Tiền Tệ - " + objCurrency.CurrencyID.ToString(), 0, ResultMessage.ErrorTypes.Insert.ToString());
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Insert, "Lỗi thêm mới Tiền Tệ", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_Currency --> Insert");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Thêm mới Tiền Tệ thành công", string.Empty);
        }

        /// <summary>
        /// Cập nhật Tiền Tệ
        /// </summary>
        /// <param name="objCurrency"></param>
        /// <returns></returns>
        public ResultMessage Update(LIS_Currency objCurrency)
        {
            ResultMessage objResultMessage = new ResultMessage();
            objResultMessage = CheckExist(objCurrency);
            if (objResultMessage.IsError) return objResultMessage;

            objResultMessage = UpdateNoCheck(objCurrency);

            return objResultMessage;
        }

        /// <summary>
        /// Cập nhật Tiền Tệ chưa kiểm tra tồn Tại
        /// </summary>
        /// <param name="objCurrency"></param>
        /// <returns></returns>
        public ResultMessage UpdateNoCheck(LIS_Currency objCurrency)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                    if (objCurrency.IsDefault)
                    {
                        var list = db.LIS_Currencys.Where(p => p.IsDefault).ToList();
                        list.ForEach(p => p.IsDefault = false);
                        db.SaveChanges();
                    }

                    LIS_Currency Currency = db.LIS_Currencys.FirstOrDefault(i => i.IDSys == objCurrency.IDSys);
                    Currency.CurrencyID = objCurrency.CurrencyID;
                    Currency.CurrencyName = objCurrency.CurrencyName;
                    Currency.ExchangeRate = objCurrency.ExchangeRate;
                    Currency.IsDefault = objCurrency.IsDefault;
                    Currency.IsActived = objCurrency.IsActived;
                    Currency.Note = objCurrency.Note;

                    db.SaveChanges();
                }
                DataCache.ClearDataCache.CurrencyCache();
                DAL_Event.Insert("Cập nhật Tiền Tệ - " + objCurrency.CurrencyID.ToString(),0, ResultMessage.ErrorTypes.Update.ToString());
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Update, "Lỗi cập nhật Tiền Tệ", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_Currency --> Update");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Cập nhật Tiền Tệ thành công", string.Empty);
        }

        /// <summary>
        /// Xóa Tiền Tệ
        /// </summary>
        /// <param name="strIDSys"></param>
        /// <returns></returns>
        public ResultMessage Delete(LIS_Currency objCurrency)
        {
            ResultMessage objResultMessage = new ResultMessage();
            objResultMessage = CheckRelationship(objCurrency);
            if (objResultMessage.IsError) return objResultMessage;

            objResultMessage = DeleteNoCheck(objCurrency);

            return objResultMessage;
        }

        /// <summary>
        /// Xóa đơn vị tiền tệ
        /// </summary>
        /// <param name="intCurrencyIDSys">Mã hệ thống đơn vị tiền tệ</param>
        /// <param name="intDeletedUser">Mã người dùng</param>
        /// <returns></returns>
        public ResultMessage Delete(int intCurrencyIDSys, int intDeletedUser)
        {
            ResultMessage objResultMessage = new ResultMessage();
            LIS_Currency objCurrency = new LIS_Currency();
            objCurrency.IDSys = intCurrencyIDSys;

            objResultMessage = CheckRelationship(objCurrency);
            if (objResultMessage.IsError) return objResultMessage;

            objResultMessage = DeleteNoCheck(objCurrency);

            return objResultMessage;
        }

        /// <summary>
        /// Xóa đơn vị tiền tệ
        /// </summary>
        /// <param name="lstCurrencyIDSys">Danh sách mã hệ thông đơn vị tiền tệ</param>
        /// <param name="intDeletedUser"></param>
        /// <returns></returns>
        public ResultMessage Delete(List<int> lstCurrencyIDSys, int intDeletedUser)
        {
            ResultMessage objResultMessage = new ResultMessage();
            foreach (int intIDSys in lstCurrencyIDSys)
            {
                LIS_Currency objCurrency = new LIS_Currency();
                objCurrency.IDSys = intIDSys;

                objResultMessage = CheckRelationship(objCurrency);
                if (objResultMessage.IsError) return objResultMessage;

                objResultMessage = DeleteNoCheck(objCurrency);
                if (objResultMessage.IsError) return objResultMessage;
            }
            return objResultMessage;
        }

        /// <summary>
        /// Kiểm tra dữ liệu liên quan
        /// </summary>
        /// <param name="strIDSys"></param>
        /// <returns></returns>
        public ResultMessage CheckRelationship(LIS_Currency objCurrency)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
               

            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.CheckData, "Lỗi kiểm tra dữ liệu liên quan Tiền Tệ", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_Currency --> CheckRelationship");
                return objResultMessage;
            }
            return objResultMessage;
        }

        /// <summary>
        /// Xóa Tiền Tệ chưa kiểm tra dữ liệu liên quan
        /// </summary>
        /// <param name="strIDSys"></param>
        /// <returns></returns>
        public ResultMessage DeleteNoCheck(LIS_Currency objCurrency)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                    LIS_Currency Currency = db.LIS_Currencys.FirstOrDefault(i => i.IDSys == objCurrency.IDSys);
                    if (Currency == null || Currency.IDSys == 0)
                    {
                        objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.No_Error, "Tiền Tệ không tồn tại", string.Empty);
                        return objResultMessage;
                    }

                    Currency.IsDeleted = true;
                    Currency.DeletedDate = DateTime.Now;

                    db.SaveChanges();
                }
                DataCache.ClearDataCache.CurrencyCache();
                DAL_Event.Insert("Xóa Tiền Tệ - " + objCurrency.CurrencyID.ToString(), 0, ResultMessage.ErrorTypes.Insert.ToString());

            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Delete, "Lỗi xóa Tiền Tệ", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_Currency --> DeleteNoCheck");
                return objResultMessage;
            }
            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Xóa Tiền Tệ thành công", string.Empty);
        }

        #endregion

        #region Load/Search/Select

        /// <summary>
        /// Lấy thông tin Tiền Tệ
        /// </summary>
        /// <param name="objCurrency"></param>
        /// <param name="strIDSys"></param>
        /// <returns></returns>
        public ResultMessage LoadInfo(ref LIS_Currency objCurrency, int strIDSys)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                    var Currency = from gu in db.LIS_Currencys
                                   where gu.IDSys == strIDSys
                                   select gu;
                    objCurrency = new LIS_Currency();
                    foreach (var obj in Currency)
                    {
                        if (!Convert.IsDBNull(obj.IDSys)) objCurrency.IDSys = obj.IDSys;
                        if (!string.IsNullOrEmpty(obj.CurrencyID)) objCurrency.CurrencyID = obj.CurrencyID;
                        if (!string.IsNullOrEmpty(obj.CurrencyName)) objCurrency.CurrencyName = obj.CurrencyName;
                        if (!Convert.IsDBNull(obj.ExchangeRate)) objCurrency.ExchangeRate = obj.ExchangeRate;
                        if (!string.IsNullOrEmpty(obj.Note)) objCurrency.Note = obj.Note;
                        if (!Convert.IsDBNull(obj.OrderIndex)) objCurrency.OrderIndex = obj.OrderIndex;
                        if (!Convert.IsDBNull(obj.IsDeleted)) objCurrency.IsDeleted = obj.IsDeleted;
                        if (!Convert.IsDBNull(obj.DeletedDate)) objCurrency.DeletedDate = obj.DeletedDate;
                    }
                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.LoadInfo, "Lấy thông tin Tiền Tệ", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_Currency --> LoadInfo");
                return objResultMessage;
            }
            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy thông tin Tiền Tệ thành công", string.Empty);
        }

        /// <summary>
        /// Lấy danh sách Tiền Tệ
        /// </summary>
        /// <param name="dtCurrency"></param>
        /// <returns></returns>
        public ResultMessage SearchData(ref DataTable dtCurrency)
        {
            ResultMessage objResultMessage = new ResultMessage();
            dtCurrency = new DataTable();

            try
            {
                using (PokerLife db = new PokerLife())
                {
                    dtCurrency = ConvertData.ConvertToDataTable<LIS_Currency>(db.LIS_Currencys.Where(i => i.IsDeleted == false).ToList());
                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.SearchData, "Lấy thông tin Tiền Tệ", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_Currency --> LoadInfo");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy danh sách Tiền Tệ thành công", string.Empty);
        }

        /// <summary>
        /// Lấy danh sách Tiền Tệ dạng LIST
        /// </summary>
        /// <param name="lstCurrency"></param>
        /// <returns></returns>
        public ResultMessage SelectToList(ref List<LIS_Currency> lstCurrency)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                    var Currency = from b in db.LIS_Currencys
                                   where b.IsDeleted == false
                                   orderby b.OrderIndex
                                   select b;
                    lstCurrency = Currency.ToList();
                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.SelectData, "Lấy thông tin Tiền Tệ", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_Currency --> LoadInfo");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy danh sách Tiền Tệ thành công", string.Empty);
        }

        /// <summary>
        /// Lấy danh sách Tiền Tệ dạng List
        /// </summary>
        /// <param name="lstUserGroup">Đối tượng Tiền Tệ</param>
        /// <param name="intPageSize">Số lượng Record một trang</param>
        /// <param name="intPageNumber">Lấy trang số mấy</param>
        /// <returns></returns>
        public ResultMessage SelectToList(ref List<LIS_Currency> lstCurrency, int intPageSize, int intPageNumber)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                    var Currency = from gu in db.LIS_Currencys
                                   where gu.IsDeleted == false
                                   orderby gu.OrderIndex
                                   select gu;

                    lstCurrency = Currency.Skip((intPageNumber - 1) * intPageSize).Take(intPageSize).ToList();
                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.SelectData, "Lấy thông tin Tiền Tệ", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_Currency --> SelectToList");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy danh sách Tiền Tệ thành công", string.Empty);
        }

        /// <summary>
        /// Lấy danh sách Tiền Tệ từ Cache
        /// </summary>
        /// <param name="lstCurrency"></param>
        /// <returns></returns>
        public ResultMessage SelectToListCache(ref List<LIS_Currency> lstCurrency)
        {
            ResultMessage objResultMessage = new ResultMessage();
            objResultMessage = DataCache.GetDataCache.GetCurrency(ref lstCurrency);
            return objResultMessage;
        }

        /// <summary>
        /// Lấy những thông tin Tiền Tệ để Cache
        /// </summary>
        /// <param name="lstCurrency"></param>
        /// <returns></returns>
        public ResultMessage SelectToCache(ref List<LIS_Currency> lstCurrency)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {

                    IEnumerable<LIS_Currency> Currency = (from gu in db.LIS_Currencys
                                                          where gu.IsDeleted == false
                                                          orderby gu.OrderIndex
                                                          select new
                                                          {
                                                              IDSys = gu.IDSys,
                                                              CurrencyID = gu.CurrencyID,
                                                              CurrencyName = gu.CurrencyName,
                                                              gu.ExchangeRate,
                                                              gu.IsDefault,
                                                              Note = gu.Note,
                                                              OrderIndex = (Nullable<int>)gu.OrderIndex
                                                          }).ToList().Select(c => new LIS_Currency()
                                                          {
                                                              IDSys = c.IDSys,
                                                              CurrencyID = c.CurrencyID,
                                                              CurrencyName = c.CurrencyName,
                                                              ExchangeRate = c.ExchangeRate,
                                                              IsDefault = c.IsDefault,
                                                              Note = c.Note,
                                                              OrderIndex = (Nullable<int>)c.OrderIndex == null ? 0 : 0
                                                          });

                    lstCurrency = Currency.ToList();
                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.SelectData, "Lấy Cache danh sách Tiền Tệ", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_Currency --> SelectToListCache");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy Cache danh sách Tiền Tệ thành công", string.Empty);
        }

        #endregion
    }
}
